package com.webxiaohua.thread.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author shenxinhua
 * @date 2019-06-23 11:57
 * @desc xxx
 */
public class ThreadPoolExecutorTest {

    public static void  testExecutor(){
        // 创建一个可缓存线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for(int i=0;i<10;i++){
            //sleep可明显看到使用的是线程池里面以前的线程，没有创建新的线程
            try{
                Thread.sleep(1000);
            }catch(InterruptedException ex){
                ex.printStackTrace();
            }
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("时间："+System.currentTimeMillis() + " "+ Thread.currentThread().getName()+"正在被执行");
                }
            });
        }
    }

    public static void main(String... args){
        testExecutor();

    }
}
